const { Service } = require('egg')

class RedisService extends Service {
  async set (key, value, seconds) {
    const { redis } = this.app
    if (seconds) { // 有过期时间
      await redis.set(key, value, 'EX', seconds)
    } else {
      await redis.set(key, value)
    }
  }

  async get (key) {
    const { redis } = this.app
    const data = await redis.get(key)
    if (data) {
      return JSON.parse(data)
    }
  }
}

module.exports = RedisService